package stockGP;

import main.Stock;
import java.util.Random;

public class Tree {

	TreeNode root;
	static int count=0;
	int interLength;
	
	public boolean testTreeOnStock(Stock s,int start)
	{
		if(root.evaluateSubtree(s, start)==1){
			return true;
		}
		else
			return false;
	}
	public Tree(Tree t){
		this.interLength=t.interLength;
		this.root=new TreeNode(t.root);
	}

	public Tree(TreeNode root, int interLength) {
		super();
		this.root = root;
		this.interLength = interLength;
	}
	
	public void printTree(){
	 root.print(0);
	}

	public Tree crossover(Tree t2){
		printTree();
		System.out.println("and the secomd tree::");
		t2.printTree();
		Random r = new Random();
		int num;

		if(interLength==1)
			num=0;
		else
			num = r.nextInt(interLength-1);
		System.out.println("num is "+num);
		TreeNode node=root.search(num);
		
		System.out.println("operation "+node.operation);
		if(node.children.size()==2)
			System.out.println("sons "+node.children.get(0).operation+","+node.children.get(1).operation);
		
		if(t2.interLength==1)
			num=0;
		else
			num = r.nextInt(t2.interLength-1);
		System.out.println("num of tree 2 is "+num);
		TreeNode node2=t2.root.search(num);
		System.out.println("operation "+node2.operation);
		if(node2.children.size()==2)
			System.out.println("sons "+node2.children.get(0).operation+","+node2.children.get(1).operation);
		
		
		node.children=node2.children;
		fixTree();
		System.out.println("_________________________________cross:");
		printTree();
		return this;
	}
	
	public void fixTree(){
		count=0;
		root.fix();
		interLength = Tree.count;
		count=0;
	}
	/*public void mutate(int MUTATION_RATE) {
		// TODO Auto-generated method stub

		Random r = new Random();
		int num;

		num = r.nextInt(MUTATION_RATE);
		// the case that this chromosome will have a mutation on him
		if(num == 0){
		
			operation=
		}
		else{
			
		}
	}*/
}
